package com.zcy;

import com.espertech.esper.common.client.EventBean;
import com.espertech.esper.runtime.client.EPEventService;
import com.zcy.util.EsperBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

/**
 * 15.4. Reading and Writing a Compiled Module
 */
@Slf4j
public class ReadWriteCompiled {

    private static final String _Time = "@name('time-statement') select sum(num) from MyEvent#win:time(2 sec)";

    /**
     * 将Compiled写入Jar文件步骤：
     * 1. {@link EsperBuilder#startCompile}的61行的false放开
     * 2. 去esper-common module中的target文件夹下的jars找到Jar包
     * 3. 通过IDEA导入外部Jar文件进行查看Jar内容
     */

    @Test
    public void lengthWindowTest() {
        EsperBuilder esperBuilder = new EsperBuilder(_Time, new Class[]{MyEvent.class});

        esperBuilder.startCompile();

        EPEventService epEventService = esperBuilder.startDeploy((eventBeans, eventBeans1, epStatement, epRuntime) -> {
            for (int i = 0; i < eventBeans.length; i++) {
                EventBean eventBean = eventBeans[i];
                log.info("sum is : {}", eventBean.get("sum(num)"));
            }
        });

        epEventService.sendEventBean(MyEvent.builder().timestamp(System.currentTimeMillis()).amount("001").name("001-name").num(10).build(), MyEvent.class.getSimpleName());
        epEventService.sendEventBean(MyEvent.builder().timestamp(System.currentTimeMillis()).amount("001").name("001-name").num(5).build(), MyEvent.class.getSimpleName());
    }


}
